// 粒子系统
function initParticles() {
    const canvas = document.getElementById('particles-canvas');
    if (!canvas) return;
    
    const ctx = canvas.getContext('2d');
    let particles = [];
    let animationId;
    
    // 设置画布大小
    function resizeCanvas() {
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
    }
    
    resizeCanvas();
    window.addEventListener('resize', resizeCanvas);
    
    // 粒子类
    class Particle {
        constructor() {
            this.reset();
            this.y = Math.random() * canvas.height;
            this.fadeDelay = Math.random() * 600;
            this.fadeStart = Date.now() + this.fadeDelay;
            this.fadingOut = false;
        }
        
        reset() {
            this.x = Math.random() * canvas.width;
            this.y = -10;
            this.vx = (Math.random() - 0.5) * 2;
            this.vy = Math.random() * 3 + 1;
            this.radius = Math.random() * 4 + 1;
            this.color = this.getRandomColor();
            this.opacity = Math.random() * 0.5 + 0.5;
            this.rotation = 0;
            this.rotationSpeed = (Math.random() - 0.5) * 0.2;
            this.type = Math.random() < 0.7 ? 'circle' : 'star';
        }
        
        getRandomColor() {
            const colors = [
                'rgba(255, 107, 157, 0.8)',
                'rgba(78, 205, 196, 0.8)',
                'rgba(255, 230, 109, 0.8)',
                'rgba(255, 138, 128, 0.8)',
                'rgba(130, 177, 255, 0.8)',
                'rgba(179, 136, 255, 0.8)'
            ];
            return colors[Math.floor(Math.random() * colors.length)];
        }
        
        update() {
            this.x += this.vx;
            this.y += this.vy;
            this.rotation += this.rotationSpeed;
            
            // 重力效果
            this.vy += 0.02;
            
            // 边界检测
            if (this.x < -10 || this.x > canvas.width + 10 || this.y > canvas.height + 10) {
                this.reset();
            }
            
            // 淡出效果
            if (Date.now() > this.fadeStart && !this.fadingOut) {
                this.fadingOut = true;
            }
            
            if (this.fadingOut) {
                this.opacity -= 0.02;
                if (this.opacity <= 0) {
                    this.reset();
                    this.fadingOut = false;
                    this.fadeStart = Date.now() + this.fadeDelay;
                }
            }
        }
        
        draw() {
            ctx.save();
            ctx.globalAlpha = this.opacity;
            ctx.translate(this.x, this.y);
            ctx.rotate(this.rotation);
            
            if (this.type === 'circle') {
                ctx.beginPath();
                ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
                ctx.fillStyle = this.color;
                ctx.fill();
            } else {
                this.drawStar();
            }
            
            ctx.restore();
        }
        
        drawStar() {
            const spikes = 5;
            const outerRadius = this.radius;
            const innerRadius = this.radius * 0.5;
            
            ctx.beginPath();
            for (let i = 0; i < spikes * 2; i++) {
                const radius = i % 2 === 0 ? outerRadius : innerRadius;
                const angle = (i * Math.PI) / spikes;
                const x = Math.cos(angle) * radius;
                const y = Math.sin(angle) * radius;
                
                if (i === 0) {
                    ctx.moveTo(x, y);
                } else {
                    ctx.lineTo(x, y);
                }
            }
            ctx.closePath();
            ctx.fillStyle = this.color;
            ctx.fill();
        }
    }
    
    // 创建粒子
    function createParticles() {
        const particleCount = Math.min(50, Math.floor(canvas.width * canvas.height / 15000));
        for (let i = 0; i < particleCount; i++) {
            particles.push(new Particle());
        }
    }
    
    // 动画循环
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        particles.forEach(particle => {
            particle.update();
            particle.draw();
        });
        
        animationId = requestAnimationFrame(animate);
    }
    
    // 鼠标交互
    let mouse = { x: 0, y: 0 };
    let mouseParticles = [];
    
    canvas.addEventListener('mousemove', (e) => {
        mouse.x = e.clientX;
        mouse.y = e.clientY;
        
        // 创建鼠标跟随粒子
        if (Math.random() < 0.3) {
            mouseParticles.push({
                x: mouse.x + (Math.random() - 0.5) * 20,
                y: mouse.y + (Math.random() - 0.5) * 20,
                vx: (Math.random() - 0.5) * 4,
                vy: (Math.random() - 0.5) * 4,
                radius: Math.random() * 3 + 1,
                color: `hsl(${Math.random() * 360}, 70%, 60%)`,
                opacity: 1,
                life: 30
            });
        }
        
        // 限制鼠标粒子数量
        if (mouseParticles.length > 20) {
            mouseParticles.shift();
        }
    });
    
    // 更新鼠标粒子
    function updateMouseParticles() {
        mouseParticles.forEach((particle, index) => {
            particle.x += particle.vx;
            particle.y += particle.vy;
            particle.vx *= 0.98;
            particle.vy *= 0.98;
            particle.opacity -= 1 / particle.life;
            particle.life--;
            
            if (particle.life <= 0) {
                mouseParticles.splice(index, 1);
            }
        });
    }
    
    // 绘制鼠标粒子
    function drawMouseParticles() {
        mouseParticles.forEach(particle => {
            ctx.save();
            ctx.globalAlpha = particle.opacity;
            ctx.beginPath();
            ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
            ctx.fillStyle = particle.color;
            ctx.fill();
            ctx.restore();
        });
    }
    
    // 修改动画循环以包含鼠标粒子
    function animateWithMouse() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        particles.forEach(particle => {
            particle.update();
            particle.draw();
        });
        
        updateMouseParticles();
        drawMouseParticles();
        
        animationId = requestAnimationFrame(animateWithMouse);
    }
    
    // 启动
    createParticles();
    animateWithMouse();
    
    // 清理函数
    window.cleanupParticles = function() {
        if (animationId) {
            cancelAnimationFrame(animationId);
        }
        particles = [];
        mouseParticles = [];
    };
}

// 特殊粒子效果
function createMagicParticles(x, y, count = 20) {
    const canvas = document.getElementById('particles-canvas');
    if (!canvas) return;
    
    const ctx = canvas.getContext('2d');
    const magicParticles = [];
    
    for (let i = 0; i < count; i++) {
        magicParticles.push({
            x: x,
            y: y,
            vx: (Math.random() - 0.5) * 10,
            vy: (Math.random() - 0.5) * 10,
            radius: Math.random() * 5 + 2,
            color: `hsl(${Math.random() * 360}, 80%, 60%)`,
            opacity: 1,
            life: 60,
            maxLife: 60
        });
    }
    
    function animateMagicParticles() {
        magicParticles.forEach((particle, index) => {
            particle.x += particle.vx;
            particle.y += particle.vy;
            particle.vx *= 0.95;
            particle.vy *= 0.95;
            particle.vy += 0.1; // 重力
            particle.opacity = particle.life / particle.maxLife;
            particle.life--;
            
            ctx.save();
            ctx.globalAlpha = particle.opacity;
            ctx.beginPath();
            ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
            ctx.fillStyle = particle.color;
            ctx.fill();
            
            // 添加发光效果
            ctx.shadowBlur = 10;
            ctx.shadowColor = particle.color;
            ctx.fill();
            
            ctx.restore();
            
            if (particle.life <= 0) {
                magicParticles.splice(index, 1);
            }
        });
        
        if (magicParticles.length > 0) {
            requestAnimationFrame(animateMagicParticles);
        }
    }
    
    animateMagicParticles();
}

